﻿using Qy.Common;
using Qy.Models;
using PetaPoco;
using PetaPoco.Core;
using System.Threading.Tasks;

namespace Qy.Repository
{
    [AppService]
    public class UserGroupRepository : BaseRepository<UserGroup, PetaPocoContext>
    {

        public UserGroupRepository(PetaPocoContext petaPocoContext) : base(petaPocoContext) { }

        /// <summary>
        /// 判断当前角色是否存在
        /// </summary>
        public async Task<bool> GetByGroupNameAsync(string groupName)
        {
            PocoData data = PocoData.ForType(typeof(UserGroup), new ConventionMapper());
            Sql sql = Sql.Builder;
            sql.Select("*");
            sql.From(new object[] { data.TableInfo.TableName });
            sql.Where("Groupname =@0", groupName);

            return await CreateDAO().FirstOrDefaultAsync<UserGroup>(sql) == null;
        }

        /// <summary>
        /// 角色分页列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<Page<UserGroup>> GetPageListAsync(int page, int limit)
        {
            PocoData data = PocoData.ForType(typeof(UserGroup), new ConventionMapper());
            Sql sql = Sql.Builder;
            sql.Select("Id,Groupname");
            sql.From(new object[] { data.TableInfo.TableName });
            sql.OrderBy("Id desc");
            return await CreateDAO().PageAsync<UserGroup>(page, limit, sql);
        }
    }
}
